package window;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import dao.*;
import entity.*;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


class MyDocument extends PlainDocument{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int maxLength;
    public MyDocument(int newMaxLength)
    {
        super();
        maxLength=newMaxLength;
    }
    public MyDocument()
    {
        this(10);
    }
    public void insertString(int offset,String str,javax.swing.text.AttributeSet a) throws BadLocationException
    {
        if(getLength()+str.length()>maxLength)
        {
            return;
        }
        else
        {
            super.insertString(offset, str,a);
        }
    }
}

public class Login {

	public JFrame LoginFrame;
	private JTextField username;
	private JPasswordField password;
	private JTextField codeField;
	private String code;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.LoginFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		LoginFrame = new JFrame();
		LoginFrame.setResizable(false);
		LoginFrame.setTitle("登录");
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		LoginFrame.setIconImage(icon.getImage());
		LoginFrame.setBounds(500, 200, 450, 300);
		LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		LoginFrame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("教师信息管理系统");
		label.setFont(new Font("幼圆", Font.BOLD, 20));
		label.setForeground(Color.DARK_GRAY);
		label.setBounds(132, 10, 180, 43);
		LoginFrame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setForeground(Color.DARK_GRAY);
		label_1.setFont(new Font("幼圆", Font.BOLD, 15));
		label_1.setBounds(82, 105, 64, 43);
		LoginFrame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("账号：");
		label_2.setForeground(Color.DARK_GRAY);
		label_2.setFont(new Font("幼圆", Font.BOLD, 15));
		label_2.setBounds(82, 63, 64, 43);
		LoginFrame.getContentPane().add(label_2);
		
		username = new JTextField();
		username.setDocument(new MyDocument(11));
		JButton login = new JButton("登录");
		username.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					login.doClick();
			}
		});
		/*username.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (keyChar >= '0' && keyChar <= '9') return ;
				Toolkit.getDefaultToolkit().beep(); //声音提示
				event.consume();
			}
		});*/
		
		
		username.setBounds(147, 73, 165, 21);
		LoginFrame.getContentPane().add(username);
		username.setColumns(10);

		password = new JPasswordField();
		password.setDocument(new MyDocument(11));
		password.addKeyListener(new KeyAdapter() {
		/*	@Override
			public void keyTyped(KeyEvent event) {
				char keyChar = event.getKeyChar();
				if (!(keyChar >= '\u4E00' && keyChar <= '\u9FA5')) return ;
				Toolkit.getDefaultToolkit().beep(); 
				event.consume();
			}*/
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					login.doClick();
			}
		});
		password.setColumns(10);
		password.setBounds(147, 115, 165, 21);
		LoginFrame.getContentPane().add(password);
		
		JButton register = new JButton("注册");
		register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Register rg=new Register();
				LoginFrame.dispose();
				rg.RegisterFrame.setVisible(true);
			}
		});
	
		register.setFont(new Font("幼圆", Font.BOLD, 12));
		register.setForeground(Color.DARK_GRAY);
		register.setBounds(219, 212, 93, 23);
		LoginFrame.getContentPane().add(register);
		
		JLabel label_3 = new JLabel("验证码：");
		label_3.setForeground(Color.GRAY);
		label_3.setFont(new Font("YouYuan", Font.BOLD, 15));
		label_3.setBounds(82, 158, 64, 43);
		LoginFrame.getContentPane().add(label_3);
		
		codeField = new JTextField();
		codeField.addKeyListener(new KeyAdapter() {
			@Override
			
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_ENTER)
						login.doClick();
				}
			
		});
		codeField.setColumns(10);
		codeField.setBounds(147, 168, 85, 21);
		LoginFrame.getContentPane().add(codeField);
		
		JLabel label_4 = new JLabel();
		label_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 1){	
					
					Object[] obj = Piccode.doGet();
					ImageIcon img = new ImageIcon((BufferedImage)obj[1]);//创建图片对象
					code=String.valueOf(obj[0]);
					label_4.setIcon((Icon)img);
					LoginFrame.getContentPane().add(label_4);
				}

			}
		});
		label_4.setHorizontalAlignment(SwingConstants.CENTER);

		Object[] objs = Piccode.doGet();
		BufferedImage image = (BufferedImage) objs[1];
		code=String.valueOf(objs[0]);
		ImageIcon img = new ImageIcon(image);
		label_4.setIcon((Icon)img);
		
		label_4.setForeground(Color.GRAY);
		label_4.setFont(new Font("YouYuan", Font.BOLD, 15));
		label_4.setBounds(245, 162, 67, 31);
		LoginFrame.getContentPane().add(label_4);

		/*login.setMnemonic(KeyEvent.VK_ENTER);*/
		login.setFont(new Font("幼圆", Font.BOLD, 12));
		login.setForeground(Color.DARK_GRAY);
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(username.getText().length()==0) JOptionPane.showMessageDialog(null, "账号为空！", "error！", JOptionPane.ERROR_MESSAGE);
				else if	(String.valueOf(password.getPassword()).equals("")) JOptionPane.showMessageDialog(null, "密码为空！", "error！", JOptionPane.ERROR_MESSAGE);
				else if	(codeField.getText().length()==0) JOptionPane.showMessageDialog(null, "验证码为空！", "error！", JOptionPane.ERROR_MESSAGE);
				/*if(TeacherDao.checkLogin(username.getText(), password.getText())) {*/
				else if (!codeField.getText().equals(code)) JOptionPane.showMessageDialog(null, "验证码错误！", "error！", JOptionPane.ERROR_MESSAGE);
				else {
					if(TeacherDao.checkLogin(username.getText(), String.valueOf(password.getPassword()))) {
				/*	System.out.println("login has succeed");*/
					welcome wc=new welcome(username.getText());
					LoginFrame.dispose();
					wc.WelFrame.setVisible(true);
				}
					else JOptionPane.showMessageDialog(null, "账号/密码错误！", "error！", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		login.setBounds(98, 212, 93, 23);
		LoginFrame.getContentPane().add(login);
		
		
	}
}
